/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.bme.j2ee.v37zen.web.beans;

import hu.bme.j2ee.v37zen.beans.FindUsersBean;
import hu.bme.j2ee.v37zen.beans.RegisterUserBean;
import hu.bme.j2ee.v37zen.util.PasswordDigest;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

/**
 *
 * @author Kiss Dániel
 */
@ManagedBean
@ViewScoped
public class RegisterUser implements Serializable{
    private static final long serialVersionUID = -3844136027486039725L;
    
    @EJB
    RegisterUserBean rub;
    @EJB
    FindUsersBean fub;
    
    private String username;
    private String email;
    private String password1;
    private String password2;

    /**
     * Creates a new instance of RegisterUser
     */
    public RegisterUser() {
    }
    
    public String submit() {
        String passwHash;
        try {
            passwHash = PasswordDigest.encodePassword(password1);   
        } catch (NoSuchAlgorithmException ex) {
            return "error.xhtml?faces-redirect=true";
        } catch (UnsupportedEncodingException ex) {
            return "error.xhtml?faces-redirect=true";
        }
        
        //System.out.println(this.email + " " + this.username + " " + this.password1 + " " + this.password2);
        rub.createUser(username, email, passwHash);
        
        return "index.xhtml?faces-redirect=true";
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public String getPassword2() {        
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public void validateUsername(FacesContext context, UIComponent toValidate, Object value){
        String username_ = (String) value;
        if(username_.length() < 3){
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("A felhasználóinév túl rövid!");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(toValidate.getClientId(context), message);
        } else if(username_.length() > 22){
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("A felhasználóinév túl hosszú!");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(toValidate.getClientId(context), message);
        }else if(! fub.isUsernameAvailable(username_)){
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Ezzel az felhasználónévvel már található regisztrált felhasználó!");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(toValidate.getClientId(context), message);
        } else {
            FacesMessage message = new FacesMessage("OK");
            message.setSeverity(FacesMessage.SEVERITY_INFO);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }


    public void validateEmail(FacesContext context, UIComponent toValidate, Object value) {
        String email_ = (String) value;
        int indexOfAt = email_.indexOf('@');
        int indexOfLastDot = email_.lastIndexOf('.');
        if (indexOfAt == -1 || indexOfLastDot == -1 || indexOfLastDot < indexOfAt ||
                indexOfAt == 0 || indexOfLastDot == email_.length()-1) 
        {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Valószínűleg hibás az emailcím!");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        } else if(! fub.isEmailAvailable(email_)){
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Ezzel az emailcímmel már található regisztrált felhasználó!");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(toValidate.getClientId(context), message);
        } else {
            FacesMessage message = new FacesMessage("OK");
            message.setSeverity(FacesMessage.SEVERITY_INFO);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
    public void validatePassword(FacesContext context, UIComponent toValidate, Object value){
        String passw1 = (String) value;
        if ( passw1.length() < 4) 
        {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("A jelszó túl rövid!");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(toValidate.getClientId(context), message);
        }
        else if ( passw1.length() > 64) 
        {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("A jelszó túl hosszú!");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(toValidate.getClientId(context), message);
        }
        else {
            FacesMessage message = new FacesMessage("OK");
            message.setSeverity(FacesMessage.SEVERITY_INFO);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
    public void confirmPassword(FacesContext context, UIComponent toValidate, Object value){
        String passw2 = (String) value;
        if (! passw2.equals(this.password1)) 
        {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("A jelszavak nem egyeznek!");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(toValidate.getClientId(context), message);
        }
        else {
            FacesMessage message = new FacesMessage("OK");
            message.setSeverity(FacesMessage.SEVERITY_INFO);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
}
